import { RawField, WidgetData, WidgetOption, WidgetType, DateSign, DateFormat, WidgetStyle } from '@/types'
import WidgetDateSign from './WidgetDateSign.vue'
import DateSignSetting from './DateSignSetting.vue'

/** 默认日期章配置 */
const SIGN_DEFAULT: DateSign = {
  fontSize: 16,
  dateFormat: DateFormat.LOCAL,
  img: ''
}

/** 签署日期 */
export default {
  key: 'date-sign',
  name: '签署日期',
  type: WidgetType.DATE,
  style: WidgetStyle.SIGN,
  icon: 'fa-icon-widget-date',
  width: 170,
  height: 30,
  component: WidgetDateSign,
  settingComponent: DateSignSetting,

  /** 初始控件数据 */
  init(widget, data) {
    widget.sign = {
      ...SIGN_DEFAULT,
      ...data.sign
    } as DateSign
  },

  /** 签署控件使用校验 */
  usingValidate(widget) {
    // 日期章未指定
    if (!widget.signImage) return `请设置${widget.option.name}`
  },

  rawWidgetCreator(widgetData, data) {
    const { fontSize, dateFormat } = data

    return Object.assign(widgetData, {
      sign: {
        fontSize: fontSize ?? SIGN_DEFAULT.fontSize,
        dateFormat: dateFormat ?? SIGN_DEFAULT.dateFormat,
        img: ''
      }
    } as WidgetData)
  },

  rawFieldGetter(widget, data) {
    const { fontSize, dateFormat } = widget.sign

    return Object.assign(data, {
      fontSize,
      dateFormat
    } as RawField)
  }
} as WidgetOption
